package com.example.springbootdemo.validator;

import com.example.springbootdemo.constant.ErrCode;
import com.example.springbootdemo.entity.UserCase;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * @author Gong.Yang
 */
@Component
public class UserCaseValidator {
    public void createValidate(UserCase userCase) {
        userCase.setCreateTime(LocalDateTime.now());
        userCase.setUpdateTime(LocalDateTime.now());
        userCase.setId(UUID.randomUUID().toString());
        entityValidator(userCase);
    }

    public void modifyValidate(UserCase userCase) {
        entityValidator(userCase);
        userCase.setUpdateTime(LocalDateTime.now());
    }

    private void entityValidator(UserCase userCase) {
        if (userCase.getAge()<=0){
            throw  ErrCode.USER_INVALID.getException("年龄不可小于0");
        }

        //TODO 其他的属性校验不做演示
    }


    public void deleteValidate( String id) {
    }
}
